package com.tfriend.sample.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import org.springframework.web.servlet.view.RedirectView;

import com.tfriend.sample.comp.sampleData;
import com.tfriend.sample.comp.sampleService;

@Controller
public class sampleController extends MultiActionController{
	
	@Autowired
	private sampleService sampleService;
	
	public ModelAndView index(HttpServletRequest request, HttpServletResponse response) {
		return new ModelAndView(new RedirectView("sample/boardList.do"));
	}
	
	public ModelAndView boardList(HttpServletRequest request, HttpServletResponse response) {
		
		List<sampleData> list = sampleService.getBoardList();
		
		return new ModelAndView("sample/boardList").addObject("list", list);
	}
	
	public ModelAndView insertForm(HttpServletRequest request, HttpServletResponse response) {
		return new ModelAndView("sample/insertForm");
	}
	
	public ModelAndView insert(HttpServletRequest request, HttpServletResponse response) throws Exception {
		request.setCharacterEncoding("UTF-8");
		
		String title = request.getParameter("title");
		String content = request.getParameter("content");
		
		sampleData data = new sampleData();
		data.setTitle(title);
		data.setContent(content);
		
		sampleService.insertContent(data);
		
		return new ModelAndView(new RedirectView("boardList.do"));
	}
	
	public ModelAndView viewContent(HttpServletRequest request, HttpServletResponse response) throws Exception {
		request.setCharacterEncoding("UTF-8");
		int no = Integer.parseInt(request.getParameter("no"));
		sampleData data = sampleService.getContent(no);
		
		return new ModelAndView("/sample/viewContent").addObject("data", data);
	}
	
	public ModelAndView delete(HttpServletRequest request, HttpServletResponse response) throws Exception {
		request.setCharacterEncoding("UTF-8");
		int no = Integer.parseInt(request.getParameter("no"));
		sampleService.deleteContent(no);
		
		return new ModelAndView(new RedirectView("boardList.do"));
	}
	
	public ModelAndView updateForm(HttpServletRequest request, HttpServletResponse response) throws Exception {
		request.setCharacterEncoding("UTF-8");
		int no = Integer.parseInt(request.getParameter("no"));
		sampleData data = sampleService.getContent(no);
		
		return new ModelAndView("sample/updateForm").addObject("data", data);
	}
	
	public ModelAndView update(HttpServletRequest request, HttpServletResponse response) throws Exception {
		request.setCharacterEncoding("UTF-8");
		int no = Integer.parseInt(request.getParameter("no"));
		String title = request.getParameter("title");
		String content = request.getParameter("content");
		
		sampleData data = new sampleData();
		data.setNo(no);
		data.setTitle(title);
		data.setContent(content);
		
		sampleService.upateContent(data);
		
		return new ModelAndView(new RedirectView("boardList.do"));
	}	
//	
//	public ModelAndView sampleData(HttpServletRequest request, HttpServletResponse response) throws Exception {
//		request.setCharacterEncoding("UTF-8");
//		StringBuffer data = sampleService.getSampleData();
//		
//		return new ModelAndView("sample/sampleData").addObject("data", data);
//	}
	
	
	@RequestMapping("/sampleData.do")
	public ModelAndView sampleData(HttpServletRequest request, HttpServletResponse response) throws Exception {
		request.setCharacterEncoding("UTF-8");
		StringBuffer data = sampleService.getSampleData();
		
		return new ModelAndView("sample/sampleData").addObject("data", data);
	}
	
}
